package org.jeecg.modules.activiti.controller;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.activiti.util.ActUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 流程控制器
 * liuzhize 2019年3月7日下午3:28:14
 */
@Slf4j
@RestController
@RequestMapping("act/processInstance")
public class ProcessInstanceController {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @GetMapping("/list")
    public Result<IPage<ProcessInstance>> list(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().listPage((pageNo - 1) * pageSize, pageNo * pageSize);
        IPage page = new Page();
        JSONArray list=new JSONArray(processInstances);
        page.setRecords(list);
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        page.setTotal(runtimeService.createProcessInstanceQuery().count());
        Result<IPage<ProcessInstance>> result = new Result();
        result.setResult(page);
        return result;
    }

    /**
     * 删除流程
     * @param id
     * @param reason
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id, @RequestParam(name = "reason", required = true) String reason) {
        runtimeService.deleteProcessInstance(id, reason);

        return Result.ok();
    }

    /**
     * 挂起流程
     * @param id
     * @return
     */
    @PostMapping(value = "/suspendProcessInstance")
    public Result<?> suspendProcessInstanceById(@RequestParam(name = "id") String id) {
        runtimeService.suspendProcessInstanceById(id);
        return Result.ok();
    }

    /**
     * 激活流程
     * @param id
     * @return
     */
    @PostMapping(value = "/activateProcessInstance")
    public Result<?> activateProcessInstance(@RequestParam(name = "id") String id) {
        runtimeService.activateProcessInstanceById(id);
        return Result.ok();
    }


    /**
     * 启动流程
     * @param key
     * @param variables
     * @return
     */
    @PostMapping(value = "/startProcessInstance")
    public Result<?> startProcessInstance(@RequestParam(name = "key") String key,@RequestBody Map variables) {
        runtimeService.startProcessInstanceByKey(key,variables);
        return Result.ok();
    }
    @GetMapping(path = "/getImage",produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] getImage(@RequestParam String id)  {
        String processInstanceId =taskService.createTaskQuery().taskId(id).singleResult().getProcessInstanceId();
        return ActUtils.getFlowImgByInstanceIdToByte(processInstanceId);
    }

}
